package com.lbtc.controller;


import com.github.pagehelper.PageInfo;
import com.lbtc.common.ApiRestResponse;
import com.lbtc.common.Constant;
import com.lbtc.domian.Category;
import com.lbtc.domian.User;
import com.lbtc.exception.LbtcMallException;
import com.lbtc.exception.LbtcMallExceptionEnum;
import com.lbtc.request.AddCategoryReq;
import com.lbtc.request.UpdateCategoryReq;
import com.lbtc.service.CategoryService;
import com.lbtc.service.UserService;
import com.lbtc.vo.CategoryVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;


@Controller
public class CategoryController {

    @Autowired
    UserService userService;
    @Autowired
    CategoryService categoryService;

    @ApiOperation("后台添加商品分类")
    @PostMapping("/admin/category/add")
    @ResponseBody
    public ApiRestResponse addCategory(HttpSession session, @RequestBody @Validated AddCategoryReq addCategoryReq)
            throws LbtcMallException {
        /*//多值空值校验，入参校验
        //设置@RequestBody注解，可以在请求的body中获取到参数
        if (addCategoryReq.getName() == null || addCategoryReq.getType() == null ||
                addCategoryReq.getParentId() == null || addCategoryReq.getOrderNum() == null) {
            //由于是
            return ApiRestResponse.error(LbtcMallExceptionEnum.PARA_NOT_NULL);
        }*/
        //身份校验
        User currentUser = (User) session.getAttribute(Constant.LBTC_MALL_USER);
        if (currentUser == null) {
            return ApiRestResponse.error(LbtcMallExceptionEnum.NEED_LOGIN);
        }
        //校验是否是管理员
        boolean adminRole = userService.checkAdminRole(currentUser);
        if (adminRole) {
            //是管理员，进行操作
            categoryService.add(addCategoryReq);
            return ApiRestResponse.success();
        } else {
            return ApiRestResponse.error(LbtcMallExceptionEnum.NEED_ADMIN);
        }
    }

    @ApiOperation("后台修改目录分类")
    @PostMapping("/admin/category/update")
    @ResponseBody
    public ApiRestResponse updateCategory(@Valid @RequestBody UpdateCategoryReq
                                                  updateCategoryReq, HttpSession session)
            throws LbtcMallException {
        User currentUser = (User) session.getAttribute(Constant.LBTC_MALL_USER);
        if (currentUser == null) {
            return ApiRestResponse.error(LbtcMallExceptionEnum.NEED_LOGIN);
        }
        //校验是否是管理员
        boolean adminRole = userService.checkAdminRole(currentUser);
        if (adminRole) {
            //是管理员，进行操作
            Category category = new Category();
            BeanUtils.copyProperties(updateCategoryReq, category);
            categoryService.update(category);
            return ApiRestResponse.success();
        } else {
            return ApiRestResponse.error(LbtcMallExceptionEnum.NEED_ADMIN);
        }
    }

    @ApiOperation("后台删除目录分类")
    @PostMapping("/admin/category/delete")
    @ResponseBody
    public ApiRestResponse deleteCategory(@RequestParam Integer id) throws LbtcMallException {
        categoryService.delete(id);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台目录分类列表")
    @PostMapping("/admin/category/list")
    @ResponseBody
    public ApiRestResponse listCategoryForAdmin(@RequestParam Integer pageNum,
                                                @RequestParam Integer pageSize) {
        PageInfo pageInfo = categoryService.listForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }


    @ApiOperation("前台目录分类列表")
    @PostMapping("/category/list")
    @ResponseBody
    public ApiRestResponse listCategoryForCustomer() {
        //List<CategoryVO> categoryVOS = categoryService.listForCustomer();
        //return ApiRestResponse.success(categoryVOS);
        List<CategoryVO> menuList = categoryService.getMenuList(0);
        return ApiRestResponse.success(menuList);
    }

}


